require(['grid', 'topBar', 'layout', 'date'], function () {
	var postData = null;
	var $gridList = $('#grid'),
	    $top = $('#top');
	$('body').layout();
	
	 
	$top.topBar({
        left: [{
            type: 'filter-more',
            dateType: 'custom',
            search: function () {
                if (getPostData()) {
                    $gridList.grid('setGridParam', {
                        datatype: "json",
                        postData: postData
                    }).trigger("reloadGrid");
                }
            },
            item: [
                {
                    text: '时间快捷',
                    name1: 'beginTime',
                    name2: 'endTime',
                    type: 'dateLine'
                }, {
                    text: '按条件汇总',
                    name: 'energyTimeType',
                    type: 'item-select',
                    data: [
                        {text: '按日汇总', value: 'day'},
                        {text: '按月汇总', value: 'month'},
                        {text: '总体汇总', value: ''}
                    ]
                }
            ]
        }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            }
        ]
    });
	
	function exportFile(suffix) {
        if ($gridList.getGridParam("reccount") > 0 && getPostData()) {
            //var $postData=$("#top").parent().getValues();
            var url = "/drivingMileage/exportDrivingMileage" + suffix + ".htm";
            $.exportFile(url, postData);
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }
	
	$gridList.grid({
        url: "/drivingMileage/searchDrivingMileage.htm",
        colModel: [
            {
                name: "drvName", sorttype: "text", align: "left", label: "驾驶员",
                cellattr: function (rowId) {
                    return 'id=\'drvName' + rowId + "\'";
                }
            },
            {
                name: "licPltNo", sorttype: "text", align: "left", label: "车牌号",
                cellattr: function (rowId) {
                    return 'id=\'licPltNo' + rowId + "\'";
                }
            },
            {
                name: "carInCd", sorttype: "text", align: "left", label: "车辆编号",
                cellattr: function (rowId) {
                    return 'id=\'carInCd' + rowId + "\'";
                }
            },
            {
                name: "lineName", sorttype: "text", align: "left", label: "营运线路",
                cellattr: function (rowId) {
                    return 'id=\'lineName' + rowId + "\'";
                }
            },
            {
                name: "organName", sorttype: "text", align: "left", label: "所属机构",
                cellattr: function (rowId) {
                    return 'id=\'organName' + rowId + "\'";
                }
            },
            {
                name: "carType", sorttype: "text", align: "left", label: "车辆类型",
                cellattr: function (rowId) {
                    return 'id=\'carType' + rowId + "\'";
                }
            },
            {
                name: "engiNo", sorttype: "text", align: "right", label: "电机型号",
                cellattr: function (rowId) {
                    return 'id=\'engiNo' + rowId + "\'";
                }
            },
            {
                name: "days", align: "center", label: "运营日期",
                cellattr: function (rowId) {
                    return 'id=\'days' + rowId + "\'";
                }
            },
            {name: "rngOprtDuraTr", align: "right", label: "运行时长"},
            {name: "rngMile", align: "right", formatter: "currency", label: "运行里程"}
        ],
        multiselect: false
    });
	
	//获取需要传递到后台的参数
    function getPostData() {
        postData = null;
        var type = 'DRIVER';
        var orgIds = commonFun.getLeftTreeCheckedNodes(type);
        if (orgIds == null || !orgIds.length) {
            $.messager.alert("提示", "请先选择左侧树的驾驶员!", "warning");
            return false;
        }
        postData = $('#top').parent().getValues();
        postData.orgIds = orgIds.toString();
        postData.type = type;
        postData.energyType = "";
        return true;
    }
	
});